package com.common.web.controller;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.UserPrincipal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.common.api.annontation.ServiceLog;
import com.common.service.SpringContextHolder;
import com.common.web.annotation.ControllerRequest;
import com.common.web.dto.response.RestResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author ficus
 *
 */
@ServiceLog
@ControllerRequest(value = "/sysApplication/")
@Api(tags = {"系统工具-应用上下文服务接口"})
public class SysApplicationController extends BaseController{
	
	@Autowired
	private SpringContextHolder	springContextHolder;
	
	@RequestMapping(value="getContext")
	@ApiOperation(value ="获取应用上下文")
	public RestResponse getContext() {
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(getRequest().getServletContext());
		RestResponse apiResult=RestResponse.success();
		apiResult.setData(context);
		return apiResult;
	}

	@RequestMapping(value="queryBeanNames")
	@ApiOperation(value ="获取bean名称集合")
	public RestResponse queryBeanNames() {
		RestResponse apiResult=RestResponse.success();
		apiResult.setData(springContextHolder.queryBeanNames());
		return apiResult;
	}	
	
	@RequestMapping(value="getBeanByName")
	@ApiOperation(value ="根据名称获取bean")
	public RestResponse getBeanByName(String beanName) {
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(getRequest().getServletContext());
		Object bean=context.getBean(beanName);
		RestResponse apiResult=RestResponse.success();
		apiResult.setData(bean.getClass());
		return apiResult;
	}	
	
	@RequestMapping(value="getBeanByClass")
	@ApiOperation(value ="根据class获取bean")
	public RestResponse getBeanByClass(String className) {
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(getRequest().getServletContext());
		Class<?> cless;
		try {
			cless = Class.forName(className);
			Object bean=context.getBean(cless);
			RestResponse apiResult=RestResponse.success();
			apiResult.setData(bean.getClass());
			return apiResult;			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			RestResponse apiResult=RestResponse.success();
			apiResult.setData(e.getException());
			return apiResult;						
		}
	}	

	@RequestMapping(value="getSystemEnvironment")
	@ApiOperation(value ="获取系统环境属性")
	public Map<String, Object> getSystemEnvironment() {
		return springContextHolder.getSystemEnvironment();
	}
	
	@RequestMapping(value="getSystemProperties")
	@ApiOperation(value ="获取系统属性")
	public Map<String, Object> getSystemProperties() {
		return springContextHolder.getSystemProperties();
	}
	
	@RequestMapping(value="getPropertyNames")
	@ApiOperation(value ="获取应用属性名称集合")
	public List<String> getPropertyNames() {
		return springContextHolder.getPropertyNames();
	}
	
	@RequestMapping(value="getPropertyValue")
	@ApiOperation(value ="获取应用属性值")
	public Object getPropertyValue(@NotNull String paramKey) {
		return springContextHolder.getPropertyValue(paramKey);
	}
	
	@RequestMapping(value="getLastModifies")
	@ApiOperation(value ="获取应用包修改时间")
	public Object getLastModifies() {
		Map<String,String> map=new HashMap<String, String>();
		ApplicationHome h2 = new ApplicationHome();
        File file=h2.getSource();
        if(file!=null) {
        	map.put("file", file.getAbsolutePath());
        	Date date = new Date(file.lastModified());
        	map.put("lastModifiedTime", DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"));
        	Path path=file.toPath();
        	try {
        		UserPrincipal user=Files.getOwner(path);
        		map.put("owner", user.getName());
        		long mills=Files.readAttributes(path, BasicFileAttributes.class).creationTime().toMillis();
        		Date createDate=new Date(mills);
        		map.put("createTime", DateFormatUtils.format(createDate, "yyyy-MM-dd HH:mm:ss"));
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
		return map;
	}
}
